package com.jindianjichi.subject.domain.service.impl;
import com.alibaba.fastjson.JSON;
import com.jindianjichi.basic.entity.SubjectCategory;
import com.jindianjichi.basic.entity.SubjectLabel;
import com.jindianjichi.basic.service.SubjectCategoryService;
import com.jindianjichi.basic.service.SubjectLabelService;
import com.jindianjichi.subject.common.enums.CategoryTypeEnum;
import com.jindianjichi.subject.common.enums.IsDeletedEnum;
import com.jindianjichi.subject.domain.convert.SubjectLabelConvert;
import com.jindianjichi.subject.domain.entity.SubjectLabelBo;
import com.jindianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
@Slf4j
public class subjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    SubjectLabelService subjectLabelService;
    @Resource
    SubjectCategoryService subjectCategoryService;
    @Override
    public void insertLabel(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.instance.convertLabelBo(subjectLabelBo);
        log.info("SubjectLabelServiceImpl.insertLabel.entity:{}", JSON.toJSONString(subjectLabel));
        subjectLabel.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        subjectLabelService.insert(subjectLabel);
    }
//逻辑删除标签
    @Override
    public void deletedLabelById(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.instance.convertLabelBo(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        subjectLabelService.update(subjectLabel);
    }

    @Override
    public void updateLabelById(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.instance.convertLabelBo(subjectLabelBo);
        subjectLabelService.update(subjectLabel);
    }

    @Override
    public List<SubjectLabelBo> selectSubjectLabelByCondition(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.instance.convertLabelBo(subjectLabelBo);
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBo.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel suLabel = new SubjectLabel();
            suLabel.setCategoryId(subjectLabelBo.getCategoryId());
            List<SubjectLabel> labelList = subjectLabelService.queryByConsition(suLabel);
            List<SubjectLabelBo> labelBoList = SubjectLabelConvert.instance.convertBoList(labelList);
            return labelBoList;
        }

        subjectLabel.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectLabel> labelList = subjectLabelService.queryByConsition(subjectLabel);
        List<SubjectLabelBo> labelBoList = SubjectLabelConvert.instance.convertBoList(labelList);
        return labelBoList;
    }
}
